<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/11/24
 * Time: 21:13
 */
namespace Task\Core;

class Database
{
    static private $db;
    static private $options;
    static private $objects;
    static private $database;

    private function __construct()
    {

    }

    /**
     * 实例化数据库
     * @param $type
     * @return bool
     */
    static function getInstance($type)
    {
        if(!$type){
            $type = 'mysql';
        }
        $config = config($type);

        //没有该数据库配置信息就返回false
        if(!$config){
            return false;
        }

        self::$options[$type] = $config;

        //注册的数据库
        self::$db = self::set($type,self::$options[$type]);
        return self::$db;
    }

    /**
     * 注册数据库对象
     * @param string $type 数据库类型
     * @param array $option 数据库的配置信息
     */
    static function set($type, $option)
    {
        if(!isset($database[$type])){
            self::$database[$type] = new Medoo($option);
        }
        return self::$database[$type];
    }

    /**
     * 获取注册的数据库对象
     * @param string $type 数据库类型
     * @return bool
     */
    static function get($type)
    {
        if (!isset(self::$database[$type]))
        {
            return false;
        }
        return self::$objects[$type];
    }

    /***
     * 释放已经注册的数据库对象
     * @param string $type 数据库类型
     */
    function _unset($type)
    {
        unset(self::$database[$type]);
    }
}